# -*- coding: utf-8 -*-
"""
@version: 1.0
@author: Pyma
@file: forms
@time: 2019/10/9 21:41
"""

from django import forms
from apps.forms import FormMixin
from django.core.cache import cache

from .models import User

# forms.Form, FormMixin 继承以后 创建的对象自然而然拥有了get_errors 方法
class LoginForm(forms.Form, FormMixin):
    telephone = forms.CharField(max_length=11)
    password = forms.CharField(max_length=20, min_length=6,error_messages={"max_length":"密码最多不能超过20个字符！","min_length":"密码最少不能少于6个字符！"})
    # 记住我  session
    remember = forms.IntegerField(required=False)

class RegisterForm(forms.Form, FormMixin):
    telephone = forms.CharField(max_length=11)
    username = forms.CharField(max_length=20)
    password1 = forms.CharField(max_length=20,min_length=6,error_messages={"max_length":"密码最多不能超过20个字符！","min_length":"密码最少不能少于6个字符！"})
    password2 = forms.CharField(max_length=20,min_length=6,error_messages={"max_length":"密码最多不能超过20个字符！","min_length":"密码最少不能少于6个字符！"})
    img_captcha = forms.CharField(max_length=4,min_length=4)
    sms_captcha = forms.CharField(max_length=4,min_length=4)

    def clean(self):
        cleaned_data = super(RegisterForm, self).clean()

        password1 = cleaned_data.get("password1")
        password2 = cleaned_data.get("password2")
        print(password1,password2,"forms获取密码")
        if password1 != password2:
            raise forms.ValidationError("两次密码输入不一致")

        img_captcha = cleaned_data.get("img_captcha")
        cache_img_captcha = cache.get(img_captcha.lower())
        print("form图形验证码",cache_img_captcha)
        if not cache_img_captcha or cache_img_captcha.lower() != img_captcha.lower():
            raise forms.ValidationError("图形验证码错误")

        telephone = cleaned_data.get("telephone")
        sms_captcha = cleaned_data.get("sms_captcha")
        cache_sms_captcha = cache.get(telephone)

        if not cache_sms_captcha or cache_sms_captcha.lower() != sms_captcha.lower():
            raise forms.ValidationError("短信验证码错误")
        exists = User.objects.filter(telephone=telephone).exists()
        if exists:
            forms.ValidationError("该手机号码已经被注册")

        return cleaned_data